package com.gr.menuw.horizontal.dropdown.multilevel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;
import org.odlabs.wiquery.ui.commons.WiQueryUIPlugin;

import com.gr.menuw.horizontal.dropdown.multilevel.css.MultiLevelMenuCssScope;
import com.gr.menuw.horizontal.dropdown.multilevel.images.MenuImageScope;
import com.gr.menuw.horizontal.dropdown.multilevel.js.MultiLevelMenuJsScope;
import com.gr.menuw.menu.IMenu;
import com.gr.menuw.menu.item.IMenuItem;


/**
 * Menu Based on dynamic drive's menu
 * @see http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/
 * @author Graham Rhodes 30 Dec 2010 15:54:55
 */
@WiQueryUIPlugin
public class MultiLevelMenu extends Panel implements IMenu, IHeaderContributor {

    private static final long serialVersionUID = 1L;
    private static final JavascriptResourceReference SLIDEMENU = new JavascriptResourceReference(MultiLevelMenuJsScope.class, "jqueryslidemenu.js");
    protected static final ResourceReference SLIDEMENU_CSS = new ResourceReference(MultiLevelMenuCssScope.class, "jqueryslidemenu.css");
    protected static final ResourceReference SLIDEMENU_CSS_BLUE = new ResourceReference(MultiLevelMenuCssScope.class, "jqueryslidemenu_blue.css");
    private static final ResourceReference RIGHT_IMG = new ResourceReference(MenuImageScope.class, "right.gif");
    private static final ResourceReference DOWN_IMG = new ResourceReference(MenuImageScope.class, "down.gif");
    private final List<IMenuItem> items = new ArrayList<IMenuItem>();
    private final WebMarkupContainer toolbar;
    private final RepeatingView itemsView;
    private boolean shadow = false;

    public MultiLevelMenu(final String id) {
        super(id);

        setRenderBodyOnly(true);

        toolbar = new WebMarkupContainer("toolbar") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(final ComponentTag tag) {
                super.onComponentTag(tag);
                String classes = "";
                if (id.contains("options-menu")) {
                    classes = (String) tag.getAttributes().get("class") + " options-menu " + id.replace("options-menu", "");
                } else
                    classes = (String) tag.getAttributes().get("class") + " " + id;
                tag.getAttributes().put("class", classes);
            }
        };

        toolbar.setOutputMarkupId(true);

        add(toolbar);

        itemsView = new RepeatingView("items");
        toolbar.addOrReplace(itemsView);

    }

    /* (non-Javadoc)
     * @see org.apache.wicket.markup.html.IHeaderContributor#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
     */
    @Override
    public void renderHead(IHeaderResponse response) {
        response.renderCSSReference(SLIDEMENU_CSS);
        response.renderJavascriptReference(SLIDEMENU);
    }

    @Override
    protected void onBeforeRender() {

        itemsView.removeAll();

        for (IMenuItem item : this.items) {
            if (item instanceof Component) {
                itemsView.add((Component) item);
            }
        }

        Label script = new Label("script", new Model<String>()) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
                String toolbarId = toolbar.getMarkupId();
                StringBuffer sb = new StringBuffer();
                sb.append("var arrowimages={down:['downarrowclass', '");
                sb.append(urlFor(DOWN_IMG));
                sb.append("', 40], right:['rightarrowclass', '");
                sb.append(urlFor(RIGHT_IMG));
                sb.append("']};");
                sb.append("jqueryslidemenu.buildmenu(\"");
                sb.append(toolbarId);
                sb.append("\", arrowimages);");
                replaceComponentTagBody(markupStream, openTag, sb.toString());
            }
        };
        toolbar.addOrReplace(script);

        super.onBeforeRender();
    }

    @Override
    public IMenu addItem(IMenuItem item) {
        items.add(item);
        return this;
    }

    @Override
    public IMenu removeItem(IMenuItem item) {
        items.add(item);
        return this;
    }


    public boolean isShadow() {
        return shadow;
    }


    public void setShadow(boolean shadow) {
        this.shadow = shadow;
    }

    @Override
    public boolean isOnTop() {
        return true;
    }

    public void hideItem(IMenuItem menuItem) {
        items.get(items.indexOf(menuItem)).show(false);
    }

    public void showItem(IMenuItem menuItem) {
        items.get(items.indexOf(menuItem)).show(true);
    }

    public void hideAllItems() {
        for (IMenuItem item : items)
            item.show(false);
    }

    public void showAllItems() {
        for (IMenuItem item : items)
            item.show(true);
    }

}
